package com.wsoft.cms.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.cms.entity.CmsAnnouncementSignEntity;
import com.wsoft.cms.mapper.CmsAnnouncementSignMapper;
import com.wsoft.cms.query.sign.SignListQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author z001
 * @Date 2024/11/27 10:42
 */
@Slf4j
@Service
public class CmsAnnouncementSignManager extends ServiceImpl<CmsAnnouncementSignMapper, CmsAnnouncementSignEntity> {


    /**
     * 根据公告id和用户id 查询
     * @param annoId 公告id
     * @param memberId 用户id
     * @return
     */
    public CmsAnnouncementSignEntity getByAnnoIdAndMemberId(Long annoId, Long memberId){
        QueryWrapper<CmsAnnouncementSignEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsAnnouncementSignEntity::getAnnoId,annoId)
                .eq(CmsAnnouncementSignEntity::getMemberId,memberId);
        return getOne(query);
    }

    /**
     * 取消报名
     * @param annoId
     * @param memberId
     * @return
     */
    public boolean cancelSign(Long annoId, Long memberId){
        if(null == annoId || null == memberId){
            return false;
        }
        QueryWrapper<CmsAnnouncementSignEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsAnnouncementSignEntity::getAnnoId,annoId)
                .eq(CmsAnnouncementSignEntity::getMemberId,memberId);
       return remove(query);
    }

    /**
     * 列表查询
     * @param param
     * @return
     */
    public List<CmsAnnouncementSignEntity> listSign(SignListQuery param) {
        QueryWrapper<CmsAnnouncementSignEntity> query = new QueryWrapper<>();
        query.lambda().eq(CmsAnnouncementSignEntity::getAnnoId,param.getAnnoId())
                .orderByDesc(CmsAnnouncementSignEntity::getSignTime);
        return list(query);
    }
}
